/*************************************************************************
    > File Name: client.cpp
    > Author: Lancer
    > Mail: 18826075768@163.com
************************************************************************/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/msg.h>
#include <string.h>

#define MAX_TEXT 512

using namespace std;

struct msg_st{
    long int msg_type;
    char text[MAX_TEXT];
};

int main(){
    int ret = 1;
    struct msg_st data;
    char buffer[BUFSIZ];
    int id = -1;
    id = msgget((key_t)1234, 0666 | IPC_CREAT);
    if(id == -1){
        fprintf(stderr, "msgget failed with error: %d\n", errno);
        exit(EXIT_FAILURE);
    }
    while(ret){
        printf("enter some text: ");
        fgets(buffer, BUFSIZ, stdin);
        data.msg_type = 1;
        strcpy(data.text, buffer);

        if(msgsnd(id, (void*)&data, MAX_TEXT, 0) == -1){
            fprintf(stderr, "msgsnd failed with error: %d\n", errno);
            exit(EXIT_FAILURE);
        }

        if(strncmp(buffer, "end", 3) == 0){
            ret = 0;
        }

        sleep(1);        
    }

    exit(EXIT_SUCCESS);
}